package com.esri.core.tasks.ags.geoprocessing;

import com.esri.core.internal.io.handler.h;
import com.esri.core.internal.tasks.TaskListener;
import com.esri.core.io.UserCredentials;
import com.esri.core.tasks.ags.geoprocessing.GPJobResource;
import java.util.Calendar;
import java.util.Map;

/* loaded from: classes.dex */
abstract class GPSubmitJobAndPollTaskCore<T> extends GPTask<T> {
    private static final long serialVersionUID = 1;
    private static final boolean stretchyPollInterval = false;
    private int _idxLastMessageReceived;
    private String _jobID;
    private GPJobStatusCallback _statusCallback;

    public GPSubmitJobAndPollTaskCore(GPTaskParameters gPTaskParameters, String str, UserCredentials userCredentials) {
        super(gPTaskParameters, str, userCredentials);
        this._idxLastMessageReceived = -1;
    }

    public GPSubmitJobAndPollTaskCore(GPTaskParameters gPTaskParameters, String str, UserCredentials userCredentials, TaskListener<T> taskListener) {
        super(gPTaskParameters, str, userCredentials, taskListener);
        this._idxLastMessageReceived = -1;
    }

    public void setStatusCallback(GPJobStatusCallback gPJobStatusCallback) {
        this._statusCallback = gPJobStatusCallback;
    }

    public GPJobResource.JobStatus getJobStatus() {
        try {
            return checkJobStatus(this._jobID).getJobStatus();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void cancel() throws Exception {
        h.a(getServiceURL() + "/jobs/" + this._jobID + "/cancel", (Map<String, String>) null, getServiceCredentials());
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public GPJobResource execInternal(boolean z) throws Exception {
        GPJobResource submitJob = submitJob();
        this._idxLastMessageReceived = 0;
        notifyUpdateStatus(submitJob);
        this._jobID = submitJob.getJobID();
        return z ? waitForCompletion(submitJob, (float) ((GPTaskParameters) getActionInput()).getStatusCallbackInterval()) : submitJob;
    }

    protected GPJobResource waitForCompletion(GPJobResource gPJobResource, float f) {
        long timeInMillis = Calendar.getInstance().getTimeInMillis();
        Calendar.getInstance().getTimeInMillis();
        Calendar.getInstance().getTimeInMillis();
        try {
            gPJobResource = checkJobStatus(this._jobID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        GPJobResource.JobStatus jobStatus = gPJobResource.getJobStatus();
        boolean z = true;
        while (z) {
            if (GPJobResource.JobStatus.CANCELLED.equals(jobStatus) || GPJobResource.JobStatus.DELETED.equals(jobStatus) || GPJobResource.JobStatus.FAILED.equals(jobStatus) || GPJobResource.JobStatus.SUCCEEDED.equals(jobStatus) || GPJobResource.JobStatus.TIMED_OUT.equals(jobStatus)) {
                z = false;
            } else if (((float) (Calendar.getInstance().getTimeInMillis() - timeInMillis)) >= f) {
                try {
                    gPJobResource = checkJobStatus(this._jobID);
                    jobStatus = gPJobResource.getJobStatus();
                    notifyUpdateStatus(gPJobResource);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                timeInMillis = Calendar.getInstance().getTimeInMillis();
            }
        }
        this._messages = gPJobResource.getMessages();
        return gPJobResource;
    }

    private void notifyUpdateStatus(GPJobResource gPJobResource) {
        if (this._statusCallback != null) {
            GPMessage[] messages = gPJobResource.getMessages();
            GPMessage[] gPMessageArr = null;
            int i = 0;
            int length = messages != null ? messages.length : 0;
            if (length > 0) {
                int i2 = this._idxLastMessageReceived;
                GPMessage[] gPMessageArr2 = new GPMessage[length - i2];
                while (i2 < length) {
                    gPMessageArr2[i] = messages[i2];
                    i2++;
                    i++;
                }
                gPMessageArr = gPMessageArr2;
            }
            this._statusCallback.statusUpdated(gPJobResource, gPMessageArr);
        }
    }

    private GPJobResource submitJob() throws Exception {
        return GPJobResource.fromJson(h.a(getServiceURL() + "/submitJob", ((GPTaskParameters) getActionInput()).generateRequestParams(), getServiceCredentials()));
    }

    private GPJobResource checkJobStatus(String str) throws Exception {
        GPTaskParameters gPTaskParameters = (GPTaskParameters) getActionInput();
        return GPJobResource.fromJson(h.a(getServiceURL() + "/jobs/" + str, (Map<String, String>) null, getServiceCredentials()));
    }
}
